﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Windows.Input;

namespace Taste
{
    /// <summary>
    /// An ICommand implementation wrapping an ITrigger object
    /// </summary>
    public static class TriggerCommand
    {
        /// <summary>
        /// Returns an ICommand object that can be bound to UI controls.
        /// </summary>
        public static ICommand AsCommand(this ITrigger trigger)
        {
            return new Command(trigger);
        }

        private class Command : ICommand
        {
            private ITrigger _trigger;

            public Command(ITrigger trigger)
            {
                _trigger = trigger;
            }

            public bool CanExecute(object parameter)
            {
                return _trigger.CanFire(parameter);
            }

            public void Execute(object parameter)
            {
                _trigger.Fire(parameter);
            }

            public event EventHandler CanExecuteChanged
            {
                add { _trigger.CanFireChanged += value; }
                remove { _trigger.CanFireChanged -= value; }
            }
        }
    }
}
